package top.infopub.mgr.cap.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.cap.FinReimDto;
import top.infopub.dto.cap.FinReimSubDto;
import top.infopub.enums.cap.CapProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.cap.dao.FinanceReimbursementDao;
import top.infopub.mgr.cap.dao.FinanceRepaySubDao;
import top.infopub.mgr.cap.jsonvo.FinReimJsonVo;
import top.infopub.mgr.cap.service.CapProcessManageService;
import top.infopub.mgr.cap.service.FinanceReimbursementService;
import top.infopub.mgr.cap.vo.FinanceReimbursementVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.service.SetupProService;
import top.infopub.model.cap.CapProcessDetail;
import top.infopub.model.cap.FinanceReimbursement;
import top.infopub.model.cap.FinanceRepaySub;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;

import java.util.Date;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/5/24 14:36
 * @Version 1.0  融资还款
 */
@Service
public class FinanceReimbursementServiceImpl extends GenericServiceImpl<FinanceReimbursement, String> implements FinanceReimbursementService {
    @Autowired
    private FinanceReimbursementDao financeReimbursementDao;

    @Autowired
    private FinanceRepaySubDao financeRepaySubDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private SetupProService setupProService;

    @Autowired
    private OAFlowService oaFlowService;

    @Autowired
    private CapProcessManageService capProcessManageService;

    @Override
    public GenericDao<FinanceReimbursement, String> getDao() {
        return financeReimbursementDao;
    }

    /*融资还款列表接口*/
    @Override
    public Page<FinanceReimbursement> selectPage(Page<FinanceReimbursement> page, FinanceReimbursementVo financeReimbursementVo) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return financeReimbursementDao.selectPage(financeReimbursementVo);
    }

    @Override
    public FinReimJsonVo queryDetailById(String id) {
        FinReimJsonVo finReimJson = new FinReimJsonVo();
        //基础信息
        FinReimDto finReimDto = financeReimbursementDao.queryDetailById(id);
        finReimJson.setInfo(finReimDto);

        //详情表格信息
        List<FinReimSubDto> detailList = financeRepaySubDao.queryListByFinRepayIdDto(finReimDto.getId());
        finReimJson.setDetailList(detailList);

        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(finReimDto.getId(), ProjectFileTypeEnum.FILE_TYPE55.getCode(), null);
        finReimJson.setFileAtt(setupProService.fileAttaListToArray(projectAttaList));
        return finReimJson;
    }

    @Override
    public RspVo edit(FinReimDto finReimDto, List<FinReimSubDto> detailList, List<ProjectAttachment> attachmentList, Integer submitType, AuthorizationVO vo) {
        //校验融资付息数据是否存在
        FinanceReimbursement tmpFr = financeReimbursementDao.selectByPrimaryKey(finReimDto.getId());
        if (tmpFr == null) return RspVo.getFailureResponse(400, "融资还款数据不存在");
        //判断审批状态
        if (StringUtils.isNotBlank(tmpFr.getProcessStatus())
                && (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), tmpFr.getProcessStatus())
                || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), tmpFr.getProcessStatus()))) {
            return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
        }

        FinanceReimbursement finReim = new FinanceReimbursement();
        finReim.setId(tmpFr.getId());
        finReim.setUptBy(vo.getId());
        finReim.setUptTime(new Date());
        finReim.setRepayAmt(finReimDto.getRepayAmt());
        //更新融资付息主表信息
        financeReimbursementDao.updateByIdSelective(finReim);

        //更新子表数据
        if (detailList != null && detailList.size() > 0) {
            for (FinReimSubDto frsd : detailList) {
                if (frsd == null) continue;
                FinanceRepaySub frs = new FinanceRepaySub();
                BeanUtils.copyProperties(frsd, frs);
                frs.setAddBy(vo.getId());
                frs.setAddTime(new Date());
                frs.setUptBy(vo.getId());
                frs.setUptTime(new Date());
                financeRepaySubDao.updateByIdSelective(frs);
            }
        }

        //更新附件信息
        projectAttachmentDao.deleteByWhere(tmpFr.getId(), ProjectFileTypeEnum.FILE_TYPE55.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }

        if (submitType == 1) {
            if (org.apache.commons.lang.StringUtils.isNotBlank(tmpFr.getOaId())) {
                oaFlowService.finishOA(tmpFr.getOaId());
            }
            String detailJson = JSON.toJSONString(queryDetailById(tmpFr.getId()));
            capProcessManageService.sendProcess(CapProcessDetailTypeEnum.PRO_NODE_TYPE17, detailJson, tmpFr.getId(), tmpFr.getProCode(), vo);
        }
        return RspVo.getSuccessResponse();
    }

    @Override
    public RspVo modifyFinInfo(FinanceReimbursement finReim, AuthorizationVO vo, String processId) {
        //校验融资付息数据是否存在
        FinanceReimbursement tmpFr = financeReimbursementDao.selectByPrimaryKey(finReim.getId());
        if (tmpFr == null) return RspVo.getFailureResponse(400, "融资还款数据不存在");
        //判断审批状态
        if (!(StringUtils.isNotBlank(tmpFr.getProcessStatus())
                && StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), tmpFr.getProcessStatus()))) {
            return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
        }

        finReim.setUptBy(vo.getId());
        finReim.setUptTime(new Date());
//        finReim.setPaymentStatus("1");
        //更新融资付息主表信息
        financeReimbursementDao.updateByIdSelective(finReim);

        CapProcessDetail cpd = capProcessManageService.queryByProcessId(processId);
        FinReimJsonVo finReimJson = JSON.parseObject(cpd.getDetailJson(), FinReimJsonVo.class);
        finReimJson.getInfo().setActRepayAmt(finReim.getActRepayAmt());
        finReimJson.getInfo().setActRepayDate(finReim.getActRepayDate());
        capProcessManageService.modifyDetailJsonById(cpd);
        return RspVo.getSuccessResponse();
    }
}
